package com.sample.thread.bizhall;

import java.util.SplittableRandom;
import java.util.concurrent.CountDownLatch;

/**
* @comments 营业窗口
* @author  zg
* @date 2021年3月1日
*/
public class BusinessWindowThread extends Thread{

    private CountDownLatch counter;
    
    //窗口编号
    private final int windowNum; 
    
    /**
     * @param windowNum 窗口编号
     */
    public BusinessWindowThread(int windowNum, CountDownLatch counter) {
        this.windowNum = windowNum;
        this.counter = counter;
    }

    @Override
    public void run() {
        Integer ticketNum = 0;
        while ((ticketNum = BusinessHall.getSeqNum()) != null) {
            System.out.println("【" + windowNum + "】号窗口 - 处理业务号码[" + ticketNum + "]...");
            int randomWait = new SplittableRandom().nextInt(1, 10);
            try {
                Thread.sleep(randomWait * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("【" + windowNum + "】号窗口 - 处理业务号码[" + ticketNum + "]执行完毕。用时" + randomWait +"秒");
        }
        counter.countDown();
    }
}
